﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


using Dapper;
using Dapper.Contrib;
using Dapper.Contrib.Extensions;
using TmsDapper;
using HYweixinOnline.Entities;
using HYweixinOnline.Common;


namespace HYweixinOnline.Admin
{
    public class SysLogBaseDAO
    {

        /// <summary>
        /// 取所有的对象
        /// </summary>
        /// <returns></returns>
        public IList<TbSysLog> GetAll()
        {

            IList<TbSysLog> tmplist;
            using (var connection = DataBase.GetOpenConnection())
            {

                tmplist = connection.Query<TbSysLog>("select * from tb_Sys_Log").ToList<TbSysLog>();


            }
            return tmplist;
        }


        /// <summary>
        /// 通过ID查询对象
        /// </summary>
        /// <param name="id">对象的ID</param>
        /// <returns></returns>
        public TbSysLog GetByID(int id)
        {
            TbSysLog curItem = null;
            using (var connection = DataBase.GetOpenConnection())
            {

                curItem = connection.Get<TbSysLog>(id);


            }

            return curItem;

        }





        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="whereClause">查询条件</param>
        /// <param name="start">起始位置</param>
        /// <param name="limit">最大条数</param>
        /// <param name="sort">排序列</param>
        /// <param name="dir">排序方向</param>
        /// <param name="count">取得记录数</param>
        /// <returns></returns>
        public IList<TbSysLog> GetByPage(string whereClause, int start, int limit, string sort, string dir, out int count)
        {

            IList<TbSysLog> tmplist;
            string Sqlstring = SQLHelper.GenPageSQL("tb_Sys_Log", whereClause, start, limit, sort, dir);
            using (var connection = DataBase.GetOpenConnection())
            {

                tmplist = connection.Query<TbSysLog>(Sqlstring).ToList<TbSysLog>();

                count = GetCount(whereClause);
            }

            return tmplist;
        }





        /// <summary>
        /// 获取分页查询的总记录条数
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public int GetCount(string where = "")
        {
            int count = 0;
            using (var connection = DataBase.GetOpenConnection())
            {
                string sqlStr = "select count(1) from tb_Sys_Log";
                if (!string.IsNullOrEmpty(where))
                {
                    sqlStr += " where " + where;
                }
                List<int> tmpList = connection.Query<int>(sqlStr).ToList<int>();
                count = tmpList[0];


            }

            return count;


        }


        /// <summary>
        /// 通过对象更新记录
        /// </summary>
        /// <param name="item">需要更新的对象</param>
        /// <returns></returns>
        public bool Update(TbSysLog item)
        {
            bool res = false;
            using (var connection = DataBase.GetOpenConnection())
            {

                res = connection.Update<TbSysLog>(item);

            }
            return res;
        }




        /// <summary>
        /// 通过ID删除记录
        /// </summary>
        /// <param name="id">需要删除的对象的ID</param>
        /// <returns></returns>
        public bool Del(int id)
        {
            bool res = false;
            TbSysLog item = new TbSysLog();
            using (var connection = DataBase.GetOpenConnection())
            {
                item = GetByID(id);
                res = connection.Delete<TbSysLog>(item);
            }
            return res;
        }

        /// <summary>
        /// 通过对象删除记录
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Del(TbSysLog item)
        {

            bool res = false;
            using (var connection = DataBase.GetOpenConnection())
            {
                res = connection.Delete<TbSysLog>(item);
            }
            return res;
        }

        /// <summary>
        /// 通过对象删除记录
        /// </summary>
        /// <param name="items">需删除的IList对象</param>
        /// <returns></returns>
        public bool Del(IList<TbSysLog> items)
        {
            bool res = false;
            using (var connection = DataBase.GetOpenConnection())
            {
                foreach (var item in items)
                {
                    connection.Delete<TbSysLog>(item);
                }
                res = true;
            }
            return res;

        }



        /// <summary>
        /// 插入一个对象
        /// </summary>
        /// <param name="item">需要删除的对象</param>
        /// <returns></returns>
        public long Insert(TbSysLog item)
        {
            long id = 0;
            using (var connection = DataBase.GetOpenConnection())
            {
                id = connection.Insert<TbSysLog>(item);
            }
            return id;
        }


        /// <summary>
        /// 插入一个IList对象
        /// </summary>
        /// <param name="items">需要删除的对象集合</param>
        /// <returns></returns>
        public bool Insert(IList<TbSysLog> items)
        {
            bool res = false;
            using (var connection = DataBase.GetOpenConnection())
            {
                foreach (var item in items)
                {
                    connection.Insert<TbSysLog>(item);
                }
                res = true;
            }
            return res;
        }

    }
}
